package com.beaven.imclient.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.beaven.imclient.R;
import com.beaven.imclient.data.ItemCategory;
import com.beaven.imclient.data.ItemTitle;
import com.beaven.imclient.data.ItemUser;
import com.beaven.imclient.utils.ImageUtil;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import de.hdodenhof.circleimageview.CircleImageView;
import java.util.List;

/**
 * author: Beaven
 * time: 2017/5/27 10:14
 *
 * 好友和群组列表Adapter
 */

public class UserAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

  public static final int TYPE_TITLE = 0x001;
  public static final int TYPE_CATEGORY = 0x002;
  public static final int TYPE_USER = 0x003;
  public static final int TYPE_GROUP = 0x004;

  private ItemTouchListener listener;
  private Context context;

  public UserAdapter(List<MultiItemEntity> data, Context context) {
    super(data);
    this.context = context;
    addItemType(TYPE_TITLE, R.layout.item_user_one);
    addItemType(TYPE_CATEGORY, R.layout.item_user_two);
    addItemType(TYPE_USER, R.layout.item_user_three);
  }

  public void setListener(ItemTouchListener listener) {
    this.listener = listener;
  }

  @Override protected void convert(final BaseViewHolder helper, final MultiItemEntity item) {
    switch (item.getItemType()) {
      case TYPE_TITLE:
        ItemTitle itemTitle = (ItemTitle) item;
        helper.setText(R.id.text_list_title, itemTitle.getTitle());
        break;
      case TYPE_CATEGORY:
        final ItemCategory itemCategory = (ItemCategory) item;
        itemCategory.setExpanded(itemCategory.hasSubItem() && itemCategory.isExpand());
        helper.setText(R.id.text_category_name, itemCategory.getName())
            .setText(R.id.text_category_status, itemCategory.getStatus())
            .setImageResource(R.id.image_arrow_category,
                itemCategory.isExpanded() ? R.drawable.arrow_bottom : R.drawable.image_arrow_black);
        LinearLayout view = helper.getView(R.id.view_user_two);
        LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (itemCategory.isExpanded()) {
          params.setMargins(ImageUtil.dip2px(context, 20), 0, 0, 0);
          view.setLayoutParams(params);
        } else {
          params.setMargins(0, 0, 0, 0);
          view.setLayoutParams(params);
        }

        helper.itemView.setOnClickListener(new View.OnClickListener() {
          @Override public void onClick(View v) {
            int pos = helper.getAdapterPosition();
            if (itemCategory.getType() == UserAdapter.TYPE_CATEGORY) {
              if (itemCategory.isExpanded()) {
                collapse(pos);
              } else {
                expand(pos);
              }
              if (itemCategory.hasSubItem()) {
                itemCategory.setExpand(itemCategory.isExpanded());
              }
            } else if (itemCategory.getType() == UserAdapter.TYPE_GROUP) {
              if (listener != null) {
                listener.onItemClick(itemCategory);
              }
            }
          }
        });
        break;
      case TYPE_USER:
        final ItemUser itemUser = (ItemUser) item;
        helper.setText(R.id.text_user_name, itemUser.getName())
            .setText(R.id.text_user_status, itemUser.getStatus());
        CircleImageView imageView = helper.getView(R.id.image_account);
        ImageUtil.show(Glide.with(context), imageView, itemUser.getAccount());
        helper.itemView.setOnClickListener(new View.OnClickListener() {
          @Override public void onClick(View v) {
            if (listener != null) {
              listener.onItemClick(itemUser);
            }
          }
        });
        helper.getView(R.id.text_right).setOnClickListener(new View.OnClickListener() {
          @Override public void onClick(View v) {
            if (listener != null) {
              listener.onRightMenuClick(itemUser);
            }
          }
        });
        break;
    }
  }

  public interface ItemTouchListener {
    void onItemClick(MultiItemEntity entity);

    void onRightMenuClick(MultiItemEntity entity);
  }
}
